package web.kx;

import java.applet.AudioClip;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.ByteBuffer;
import java.util.Hashtable;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

import org.apache.log4j.Logger;

public class Notifer {

	/**
	 * @param args
	 * @throws Exception
	 * @throws IOException
	 * @throws UnsupportedAudioFileException
	 * @throws LineUnavailableException
	 * @throws InterruptedException
	 */
	public static void main(String[] args) throws Exception {
		Notifer.error();
		Thread.sleep(5000);
	}

	static String URL = "http://sms.api.bz/fetion.php?username="+Config.getProperty("飞信手机")
	+"&password="+Config.getProperty("飞信密码")+"&sendto="+Config.getProperty("飞信手机")+"&message=kaixin001Error";
	static int sum=0;
	public static synchronized void error() {
		if (errTime == -1 || System.currentTimeMillis() - errTime >= 90 * 1000) {
			if (errTime == -1
					|| System.currentTimeMillis() - errTime >= 90 * 1000) {
				errTime = System.currentTimeMillis();
				logger.info("pls login wap to verify");
				sum=1;
			} else {
				logger.info("duplicate error,omit " + sum++ +" times");
				return;
			}
			try {
				URLConnection openConnection = new URL(URL).openConnection();
				InputStream inputStream = openConnection.getInputStream();
				int i = -1;
				byte [] bs = new byte[1000];
				int x=0;
				for(; (-1 != (i = inputStream.read()));x++ ){
					bs[x]=(byte)i;
				}
				String string = new String(bs, 0, x-1, "UTF-8");
				logger.info(string);
			} catch (Exception e) {
				logger.error(e);
			}
			try {
				java.net.URI thisuri=new java.net.URI("http://wap.kaixin001.com/cafe/index.php");
				java.awt.Desktop.getDesktop().browse(thisuri);
			} catch (URISyntaxException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			Clip c = null;
			try {
				c = getClip(WARNING);
			} catch (Exception e) {
				e.printStackTrace();
			}
			c.loop(10);
		}

	}

	private final static String WARNING = "warning";
	private final static String INFO = "info";
	private final static String ERROR = "error";
	private final static Hashtable<String, Clip> ht = new Hashtable<String, Clip>();
	private final static Hashtable<String, String> files = new Hashtable<String, String>();
	static {
		files.put(WARNING, "C:\\Windows\\Media\\chimes.wav");
		files.put(INFO, "C:\\Windows\\Media\\ding.wav");
		files.put(ERROR, "C:\\Windows\\Media\\chord.wav");
	}
	private final static Logger logger = Main.logger;

	private static Clip getClip(String s) throws Exception {
		Clip c = ht.get(s);
		if (c == null) {
			AudioInputStream stream;
			stream = AudioSystem.getAudioInputStream(new File(files.get(s)));
			// At present, ALAW and ULAW encodings must be converted
			// to PCM_SIGNED before it can be played
			AudioFormat format = stream.getFormat();
			if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
				format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
						format.getSampleRate(),
						format.getSampleSizeInBits() * 2, format.getChannels(),
						format.getFrameSize() * 2, format.getFrameRate(), true); // big
																					// endian
				stream = AudioSystem.getAudioInputStream(format, stream);
			}
			// Create the clip
			DataLine.Info info = new DataLine.Info(Clip.class, stream
					.getFormat(), ((int) stream.getFrameLength() * format
					.getFrameSize()));
			c = (Clip) AudioSystem.getLine(info);
			// This method does not return until the audio file is
			// completely loaded
			c.open(stream);
			// Start playing
			ht.put(s, c);
		}
		return c;
	}

	private static long errTime = -1;

	public static void warning() {
		logger.error("warning be called");
		if (errTime == -1 || System.currentTimeMillis() - errTime >= 90 * 1000) {
			synchronized (Notifer.class) {
				if (errTime == -1
						|| System.currentTimeMillis() - errTime >= 90 * 1000) {
					errTime = System.currentTimeMillis();
				} else {
					return;
				}
			}
			Clip c = null;
			try {
				c = getClip(WARNING);
			} catch (Exception e) {
				e.printStackTrace();
			}
			c.loop(10);
		}
	}

	public static void reSchdule() {
		System.exit(-1);
	}

	public static void info() {
		logger.error("info be called");
		Clip c = null;
		try {
			c = getClip(INFO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		c.loop(10);
	}
}
